package com.zr.legalperson.service.impl;

import com.zr.legalperson.mapper.LegalPersonMapper;
import com.zr.legalperson.model.LegalPerson;
import com.zr.legalperson.model.Plant;
import com.zr.legalperson.model.PlantSelectResultVo;
import com.zr.legalperson.model.PlantSelectVo;
import com.zr.legalperson.model.vo.PlantAddVo;
import com.zr.legalperson.model.vo.PlantResultVo;
import com.zr.legalperson.model.vo.PlantUpdateStatusVo;
import com.zr.legalperson.model.vo.PlantUpdateVo;
import com.zr.legalperson.service.LegalPersonService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/1/3.
 */
@Service
public class LegalPersonServiceImpl implements LegalPersonService{

    @Autowired
    private LegalPersonMapper legalPersonMapper;

    @Override
    public ResultVO<List<LegalPerson>> legalPersonDropDownValue() {
        List<LegalPerson> legalPeople = legalPersonMapper.legalPersonDropDownValue();
        return ResultVOBuilder.success(legalPeople);
    }

    @Override
    public ResultVO<AllRecords> legalPlantQuery(PlantSelectVo plantSelectVo) {

        int count = legalPersonMapper.queryCount(plantSelectVo);
        List<PlantSelectResultVo> plantSelectResultVos = legalPersonMapper.queryPage(plantSelectVo);

        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(plantSelectResultVos);
        allRecords.setPageIndex(plantSelectVo.getPageIndex());
        allRecords.setPageSize(plantSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);

        return ResultVOBuilder.success(allRecords);

    }

    @Override
    public ResultVO<PlantAddVo> legalPlantAdd(PlantAddVo plantAddVo) {
        Date nowDate = new Date();
        Plant plant = new Plant();
        BeanUtils.copyProperties(plantAddVo,plant);

        plant.setCreateId(1024);
        plant.setCreateName("于江");
        plant.setCreateTime(nowDate);
        plant.setUpdateId(1024);
        plant.setUpdateName("于江");
        plant.setUpdateTime(nowDate);

        //1.验证工厂编码是否重复
        List<Plant> plants = legalPersonMapper.queryPlantByCode(plantAddVo.getLegalPlantCode());
        if (!CollectionUtils.isEmpty(plants)){
            return ResultVOBuilder.error("500","法人编码已经存在，请重新输入！");
        }

        legalPersonMapper.legalPlantAdd(plant);
        return ResultVOBuilder.success(plantAddVo);
    }

    @Override
    public ResultVO<PlantUpdateStatusVo> legalPlantUpdatestatus(@Valid PlantUpdateStatusVo updateStatusVo) {


        Plant plant = new Plant();

        Date nowData = new Date();
        Plant plant1 = new Plant();
        plant1.setUpdateId(1024);
        plant1.setUpdateName("于江");
        plant1.setUpdateTime(nowData);

        BeanUtils.copyProperties(updateStatusVo,plant);
        legalPersonMapper.legalPlantUpdatestatus(plant);
        return ResultVOBuilder.success(updateStatusVo);
    }
    @Override
    public ResultVO<PlantUpdateVo> legalPlantUpdate(@Valid PlantUpdateVo plantUpdateVo) {

        //        通过工厂编码查要修改的对象存在不
        List<Plant> plants = legalPersonMapper.queryPlantByCode(plantUpdateVo.getLegalPlantCode());
        if (plants.isEmpty()){

            return ResultVOBuilder.error("500","要修改的工厂不存在，请重新输入");
        }

        Date nowData = new Date();
        Plant plant1 = new Plant();
        plant1.setUpdateId(1024);
        plant1.setUpdateName("于江");
        plant1.setUpdateTime(nowData);

        BeanUtils.copyProperties(plantUpdateVo,plant1);
        legalPersonMapper.legalPlantUpdate(plant1);
        return ResultVOBuilder.success(plantUpdateVo);
    }

}
